﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.RegexUtil
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System.Collections.Generic;
using System.Text.RegularExpressions;

#nullable enable
namespace Commom.Util;

public static class RegexUtil
{
  public static Dictionary<string, string> GetValues(
    string input,
    string pattern,
    string[] resultPatterns,
    RegexOptions options = RegexOptions.IgnoreCase)
  {
    Dictionary<string, string> result = new Dictionary<string, string>();
    if (string.IsNullOrWhiteSpace(input))
      return result;
    Match match = Regex.Match(input, pattern, options);
    if (!match.Success)
      return result;
    RegexUtil.AddResults(result, match, resultPatterns);
    return result;
  }

  private static void AddResults(
    Dictionary<string, string> result,
    Match match,
    string[] resultPatterns)
  {
    if (resultPatterns == null)
    {
      result.Add(string.Empty, match.Value);
    }
    else
    {
      foreach (string resultPattern in resultPatterns)
        result.Add(resultPattern, match.Result(resultPattern));
    }
  }

  public static string GetValue(
    string input,
    string pattern,
    string resultPattern = "",
    RegexOptions options = RegexOptions.IgnoreCase)
  {
    if (string.IsNullOrWhiteSpace(input))
      return string.Empty;
    Match match = Regex.Match(input, pattern, options);
    if (!match.Success)
      return string.Empty;
    return !string.IsNullOrWhiteSpace(resultPattern) ? match.Result(resultPattern) : match.Value;
  }

  public static string[] Split(string input, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
  {
    return string.IsNullOrWhiteSpace(input) ? new string[0] : Regex.Split(input, pattern, options);
  }

  public static string Replace(
    string input,
    string pattern,
    string replacement,
    RegexOptions options = RegexOptions.IgnoreCase)
  {
    return string.IsNullOrWhiteSpace(input) ? string.Empty : Regex.Replace(input, pattern, replacement, options);
  }

  public static bool IsMatch(string input, string pattern)
  {
    return RegexUtil.IsMatch(input, pattern, RegexOptions.IgnoreCase);
  }

  public static bool IsMatch(string input, string pattern, RegexOptions options)
  {
    return Regex.IsMatch(input, pattern, options);
  }
}
